<?php
 require_once(dirname(__FILE__).'/../include/config.inc.php');



// 工单列表
$sql = "SELECT id,driving_distance FROM `#@__order` WHERE  delstate='' AND qiandao='true' AND  driving_distance='0'  order by id desc limit 2   ";
$dosql->Execute($sql,8);
while($row = $dosql->GetArray(8))
{
    echo "<br>==============order ".$row['id']."==============<br>";
    // 获取 接单位置
	$lat1="";
	$lng1="";
	$lat2="";
	$lng2="";
	$aa=0;
	$total_distance=0;
	$order_id=$row['id'];
	$n=0;
	$new_array=array();
	$row_j = $dosql->Execute("SELECT * FROM `#@__order_log` WHERE `orderid`='$order_id'  ",$order_id);
    while($row_j = $dosql->GetArray($order_id))
	{
		$new_array[$n]=$row_j;
	    if($row_j['qiandao']=="true" ){
	        $log_id=$row_j['id'];
			$lat1=$row_j['latitude'];
			$lng1=$row_j['longitude'];
            $lat2=$new_array[$n-1]['latitude'];
            $lng2=$new_array[$n-1]['longitude'];

            echo "<br>".$new_array[$n-1]['op']."位置：".$lat2.",".$lng2;
            echo "<br>".$row_j['op']."位置：".$lat1.",".$lng1;
            $aa= driving_distance($lng2, $lat2, $lng1, $lat1);
            $dosql->ExecNoneQuery("update  `#@__order_log` set driving_distance='$aa' where id='$log_id' ");
            echo "<br>导航距离".$aa;
            if($aa>0){
                $total_distance=$total_distance+$aa;
            }
            echo "<br>合计导航距离".$total_distance;
            $dosql->ExecNoneQuery("update  `#@__order` set driving_distance='$total_distance' where id='$order_id' ");
		}
        $n++;

	}
    echo "<br>============================<br>";
    var_dump($new_array);
    echo "<br>============================<br>";

	//echo $lat1.",".$lng1."    ".$lat2.",".$lng2."<br>";
	//$aa= driving_distance($lng1, $lat1, $lng2, $lat2);
	//$dosql->ExecNoneQuery("update  `#@__order` set driving_distance='$aa' where id='$order_id' ");

  // 获取  签到位置

}




// 计算导航距离
#lon为经度，lat为纬度，一定不要弄错了哦
function driving_distance($lng1, $lat1, $lng2, $lat2){
	$distance="";
	$url='https://apis.map.qq.com/ws/direction/v1/driving/?from='.$lat1.','.$lng1.'&to='.$lat2.','.$lng2.'&policy=LEAST_TIME&output=json&key=DRXBZ-Q3CHG-6KOQN-IJY6G-LDCS5-W4BZZ';

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	//curl_setopt($ch, CURLOPT_HEADER, 1);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
	//curl_setopt($ch, CURLOPT_POST, 1);
	//curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
	$html = curl_exec($ch);
	$json_arr=json_decode($html,true);
	if($json_arr['status']==0){
		$distance=$json_arr['result']['routes'][0]['distance'];
	}
	return $distance;

}



?>